
package net.swa.dewei.web.action;

import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.DingDanCard;
import net.swa.dewei.service.DingDanCardService;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.BuzConst;
import net.swa.util.JsonResult;

/**一卡通商品订单信息表 Action**/
public class DingDanCardAction extends AbstractBaseAction
{
    private static final long serialVersionUID = 6248407300486551422L;

    private DingDanCardService dingDanCardService;

    private DingDanCard model;

    //实体类id
    private Long id;

    //门店id
    private Long shopId;

    //批量删除的id集合
    private Long[] ids;

    private String status;

    //用户类型
    private Short userType;

    /**分页中订单id**/
    private String dingDanUid;

    /**订单状态***/
    private String state;

    /**更换方式***/
    private String payType;

    private Long fuwuZhanId;

    /**当前用户若是服务站，其服务站id**/
    private Long fuwuzhanId;

    //客户名
    private String customerName;

    //联系方式、
    private String customerTel;

    //门店名称
    private String mendianName;

    private String fuwuzhanName;

    /** 公用方法接口 */
    private ICommonService commonService;

    @Log(description = "初始化分页查询(支持不同数据范围查询)")
    public String initQuery()
    {
        //        User user = getCurrentUser();
        return "queryList";
    }

    public String queryList() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        JsonResult<DingDanCard> json = null;
        User user = getCurrentUser();
        if (null != user)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
            {
                fuwuZhanId = user.getFuwuzhanId();
            }
            else
            {
                fuwuZhanId = null;
            }
        }
        json = dingDanCardService.queryDingdansPage(customerName, customerTel, mendianName, dingDanUid, state, payType, fuwuZhanId, fuwuzhanName, currentPage, pageSize, orderBy, orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    /**去新增或者修改页面**/
    @Log(description = "去新增或者修改订单页面")
    public String toAddDdLife() throws Exception
    {
        User user = getCurrentUser();
        if (null != user)
        {
            userType = user.getUserType();
            if (null != id)
            {
                model = dingDanCardService.queryById(id);
            }
            else
            {
                model = new DingDanCard();
            }
            return "toAddDingDan";
        }
        else
        {
            return LOGIN;
        }
    }

    /** * 删除方法
     * @throws Exception **/
    @Log(description = "更改订单信息")
    public String saveDingDan() throws Exception
    {
        dingDanCardService.update(model);
        outSuccess();
        return null;
    }

    /** * 删除方法**/
    @Log(description = "删除订单")
    public String delete() throws Exception
    {
        dingDanCardService.delete(ids);
        outSuccess();
        return null;
    }

    /**更改订单状态
     * @throws Exception */
    public String updateOrderStatus() throws Exception
    {
        if (null == id || StringUtils.isBlank(status))
        {
            outError("参数不合法");
        }
        else
        {
            Long[] ids = new Long[1];
            ids[0] = id;
            dingDanCardService.batchUpdateState(ids, status);
            outSuccess();
        }
        return null;
    }

    public String assign() throws Exception
    {
//        if (null == id || null == shopId)
//        {
//            outError("参数不合法");
//        }
//        else
//        {
//            Map<String, Object> map = dingDanCardService.updateProcess(id, BuzConst.CARD_DINGDAN_STATES2, shopId);
//            if (null == map)
//            {
//                outError("参数不合法");
//            }
//            else
//            {
//                outJson(map);
//            }
//        }
        return null;
    }

    /**
     * 指派 信息
     * @return
     */
    public String zhipai()
    {
        User u = getCurrentUser();
        if (null != u)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == u.getUserType())
            {
                if (null != u.getFuwuzhanId())
                {
                    fuwuzhanId = u.getFuwuzhanId();
                }
            }
            return "zhipai";
        }
        else
        {
            return LOGIN;
        }
    }

    //导出跟踪记录的excel
    public String exportExcel() throws Exception
    {
        //        User user = getCurrentUser();
        //        if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
        //        {
        //            fuwuZhanId = user.getFuwuzhanId();
        //        }
        //        else
        //        {
        //            fuwuZhanId = null;
        //        }
        //        HSSFWorkbook workbook = dingDanLifeGoodsService.exportExcel(dingDanUid, state, payType, fuwuZhanId);
        //        HttpServletResponse response = getResponse();
        //        BufferedOutputStream bos = null;
        //        try
        //        {
        //            response.setContentType("application/vnd.ms-excel");
        //            String filename = new String("filename".getBytes("gbk"), "iso-8859-1") + ".xls";
        //            response.setHeader("Content-disposition", "attachment;filename=" + filename);
        //            bos = new BufferedOutputStream(response.getOutputStream());
        //            workbook.write(bos);
        //            bos.flush();
        //            removeSessionAttribute("listSheet");
        //        }
        //        catch (Exception e)
        //        {
        //            e.printStackTrace();
        //        }
        //        finally
        //        {
        //            bos.close();
        //        }
        return null;
    }

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public Long getShopId()
    {
        return shopId;
    }

    public void setShopId(Long shopId)
    {
        this.shopId = shopId;
    }

    public String getDingDanUid()
    {
        return dingDanUid;
    }

    public void setDingDanUid(String dingDanUid)
    {
        this.dingDanUid = dingDanUid;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public String getPayType()
    {
        return payType;
    }

    public void setPayType(String payType)
    {
        this.payType = payType;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    public Long getFuwuZhanId()
    {
        return fuwuZhanId;
    }

    public void setFuwuZhanId(Long fuwuZhanId)
    {
        this.fuwuZhanId = fuwuZhanId;
    }

    public Short getUserType()
    {
        return userType;
    }

    public void setUserType(Short userType)
    {
        this.userType = userType;
    }

    public Long getFuwuzhanId()
    {
        return fuwuzhanId;
    }

    public void setFuwuzhanId(Long fuwuzhanId)
    {
        this.fuwuzhanId = fuwuzhanId;
    }

    public String getCustomerName()
    {
        return customerName;
    }

    public void setCustomerName(String customerName)
    {
        this.customerName = customerName;
    }

    public String getCustomerTel()
    {
        return customerTel;
    }

    public void setCustomerTel(String customerTel)
    {
        this.customerTel = customerTel;
    }

    public String getMendianName()
    {
        return mendianName;
    }

    public void setMendianName(String mendianName)
    {
        this.mendianName = mendianName;
    }

    public String getFuwuzhanName()
    {
        return fuwuzhanName;
    }

    public void setFuwuzhanName(String fuwuzhanName)
    {
        this.fuwuzhanName = fuwuzhanName;
    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    public DingDanCard getModel()
    {
        return model;
    }

    public void setModel(DingDanCard model)
    {
        this.model = model;
    }

    @Required
    @Resource
    public void setDingDanCardService(DingDanCardService dingDanCardService)
    {
        this.dingDanCardService = dingDanCardService;
    }

}
